package net.kazed.nextaction.task;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import net.kazed.nextaction.R;
import net.kazed.nextaction.context.activity.ContextsActivity;
import net.kazed.nextaction.database.GtdContext;
import net.kazed.nextaction.database.Task;
import net.kazed.nextaction.tools.PurgeActivity;

public class TaskViewActivity extends Activity {

    private static final String cTag = "ActionViewActivity";
    
    private static final int MENU_EDIT = Menu.FIRST;
    private static final int MENU_DELETE = Menu.FIRST + 1;
    private static final int MENU_MARK_COMPLETED = Menu.FIRST + 2;
    private static final int MENU_CHANGE_CONTEXT = Menu.FIRST + 3;
   
    private static final int ACTIVITY_EDIT_ACTION = 0;
    
    private Cursor cursor;
    private Uri itemUri;
    private Task originalTask;
    private TextView nameWidget;
    private TextView statusText;
    private TextView noteWidget;
        
    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        final Intent intent = getIntent();
        itemUri = intent.getData();
        cursor = managedQuery(itemUri, Task.FULL_PROJECTION.getFieldNames(), null, null, null);
        
        setContentView(R.layout.task_view);
        nameWidget = (TextView) findViewById(R.id.description);
        statusText = (TextView) findViewById(R.id.status);
        noteWidget = (TextView) findViewById(R.id.notes);
    }
    
    @Override
    protected void onResume() {
        Log.d(cTag, "onResume+");
        super.onResume();

        if (cursor != null) {
            cursor.moveToFirst();
            originalTask = new Task(cursor);
            populateFields(originalTask);
        }
    }

   private void populateFields(Task task) {
      nameWidget.setText(task.getDescription() == null ? "" : task.getDescription());
      String status = null;
      if (task.getComplete()) {
         status = getResources().getString(R.string.status_completed);
      } else {
         status = getResources().getString(R.string.status_in_progress);
      }
      statusText.setText(status);
      noteWidget.setText(task.getDetails());
   }
    
    @Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent data) {
    	Log.d(cTag, "Got resultCode " + resultCode + " with data " + data);		
	}
        
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       Resources resources = getResources();
       menu.add(0, MENU_EDIT, 0, resources.getString(R.string.menu_edit))
          .setIcon(android.R.drawable.ic_menu_edit).setAlphabeticShortcut('e');
       menu.add(0, MENU_DELETE, 0, resources.getString(R.string.menu_delete))
          .setIcon(android.R.drawable.ic_menu_delete).setAlphabeticShortcut('d');
       menu.add(0, MENU_MARK_COMPLETED, 0, resources.getString(R.string.menu_mark_completed))
          .setIcon(android.R.drawable.ic_menu_close_clear_cancel).setAlphabeticShortcut('c');
//       menu.add(0, MENU_CHANGE_CONTEXT, 0, resources.getString(R.string.menu_change_context))
//          .setIcon(android.R.drawable.ic_menu_close_clear_cancel).setAlphabeticShortcut('c');
       return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       switch (item.getItemId()) {
       case MENU_EDIT:
          Intent intent = new Intent(Intent.ACTION_EDIT, itemUri);
          startActivityForResult(intent, ACTIVITY_EDIT_ACTION);
          break;
       case MENU_DELETE:
          getContentResolver().delete(itemUri, null, null);
          finish();
          break;
       case MENU_MARK_COMPLETED:
          originalTask.setComplete(true);
          ContentValues values = new ContentValues();
          originalTask.toValues(values);
          getContentResolver().update(itemUri, values, null, null);
          finish();
          break;
       case MENU_CHANGE_CONTEXT:
          break;
       }
       return super.onOptionsItemSelected(item);
    }
    
}
